﻿using System;
using System.Collections.Generic;
using System.Linq;
using Schedulr.Models;

namespace Schedulr.Infrastructure
{
    /// <summary>
    /// Provides extensions methods on various types.
    /// </summary>
    public static class SchedulrExtensions
    {
        #region Nullable Extensions

        /// <summary>
        /// Gets the value of the specified <see cref="Nullable{T}"/>, or the specified default value if it has no value.
        /// </summary>
        /// <typeparam name="T">The nullable type.</typeparam>
        /// <param name="value">The nullable value.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>The value of the specified <see cref="Nullable{T}"/>, or the specified default value if it has no value.</returns>
        public static T ValueOr<T>(this Nullable<T> value, T defaultValue) where T : struct
        {
            if (value.HasValue)
            {
                return value.Value;
            }
            else
            {
                return defaultValue;
            }
        }

        #endregion

        #region Picture Extensions

        /// <summary>
        /// Gets the lowest batch of pictures from the specified list.
        /// </summary>
        /// <param name="pictures">The pictures to retrieve the lowest numbered batch from.</param>
        /// <returns>The pictures with the lowest batch number.</returns>
        public static IList<Picture> GetLowestBatch(this ICollection<Picture> pictures)
        {
            if (pictures == null || pictures.Count == 0)
            {
                return new Picture[0];
            }
            else
            {
                var batchId = pictures.Min(p => p.BatchId);
                return pictures.Where(p => p.BatchId == batchId).ToList();
            }
        }

        #endregion
    }
}